

// Include the correct display library
#include<Servo.h>                  //加载舵机库
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>


const char* ssid = "esp8266";
const char* password = "esp8266cil";
WiFiUDP Udp;
unsigned int localUdpPort = 4444;  // local port to listen on
char incomingPacket[255];  // buffer for incoming packets
char  replyPacket[] = "Hi there! Got the message :-)";  // a reply string to send back

Servo myservo;                     //定义一个舵机对象
Servo myservo2; 
int _servo = 4;                   //指定IO14(D5)引脚
int _servo2 = 5;  

#define UP_KEY      13 //gpio12
#define RIGHT_KEY   12 //gpio14
#define DOWN_KEY    14 //gpio13
#define LEFT_KEY    0 //gpio13
 
//读取键值
#define keyup     digitalRead(UP_KEY)
#define keyright  digitalRead(RIGHT_KEY)
#define keydown   digitalRead(DOWN_KEY)
#define keyleft   digitalRead(LEFT_KEY)


int fast = 1;
int angle = 90; 


void setAngle(String context,int angle, int fast){
      digitalWrite(2,HIGH);
      Serial.println(context);  
      if (context == "up")
        for(int i=180;i>=angle;i=i-fast)             //控制舵机角0~90°
        {
          myservo2.write(i);               
        }
      if (context == "down")
        for(int i=0;i<angle;i=i+fast)             //控制舵机角0~90°
      {
        myservo2.write(i);                
      }
      if (context == "left")
        for(int i=0;i<angle;i=i+fast)             //控制舵机角0~90°
      {
        myservo.write(i);                
      
      }      
      if (context == "right")
        for(int i=180; i>=angle;i=i-fast)             //控制舵机角0~90°
      {
        myservo.write(i);                
  
      }
      delay(300);
      digitalWrite(2,LOW);
        
}
void setup() {
  myservo.attach(_servo); 
  myservo2.attach(_servo2); 

  pinMode(UP_KEY, INPUT_PULLUP);
  pinMode(RIGHT_KEY, INPUT_PULLUP);
  pinMode(DOWN_KEY, INPUT_PULLUP);
  pinMode(LEFT_KEY, INPUT_PULLUP);
  
  Serial.begin(115200);
  Serial.println();
  digitalWrite(2,LOW);
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  int i = 0;
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(100);
    i += 1;
    Serial.print(".");
    if (i == 50){
      break;
    }
  }
  Serial.println(" connected");
  pinMode(2, OUTPUT);
  Udp.begin(localUdpPort);
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}


void loop() {
  

  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    // receive incoming UDP packets
    Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
    int len = Udp.read(incomingPacket, 255);
    // Serial.printf("%s",len);
    if (len > 0)
    {
      incomingPacket[len] = 0;
    }
    Serial.printf("Check Result\n");
    String context;
    context = incomingPacket;
    int fast = 1;
    int angle = 90; 
    setAngle(context,angle,fast);
    strcpy(incomingPacket,"none");

    // Serial.printf("UDP packet contents: %s\n", incomingPacket);
  
    // send back a reply, to the IP address and port we got the packet from
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(replyPacket);
    Udp.endPacket();
  }  
  if((keyup==0)||(keydown==0)||(keyright==0)||(keyleft==0))
	{

		delay(10);//消抖
		if(keyup==0)
		{
       setAngle("up",angle,fast);
       Serial.println("up"); 
			// while(!keyup)ESP.wdtFeed();// 喂 狗    
		}
		if(keydown==0)
  	{
      setAngle("down",angle,fast);
      Serial.println("down");   
    	// while(!keydown)ESP.wdtFeed();// 喂 狗    
		}
    if(keyright==0)                     
		{            
      setAngle("right",angle,fast); 
      Serial.println("right");  
		  // while(!keyright)ESP.wdtFeed();// 喂 狗
		}
    if(keyleft==0)                     
		{        
      setAngle("left",angle,fast);
      Serial.println("left");    
		  // while(!keyleft)ESP.wdtFeed();// 喂 狗
		}
	}
  delay(100);
  // ESP.wdtFeed();// 喂 狗     


}
